package com.ohs.dal.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.ohs.bll.util.Constant;
import com.ohs.dal.dao.LocationDAO;
import com.ohs.dal.model.Location;
import com.ohs.dal.model.Province;

public class LocationDAOImpl extends AbstractDAOImpl<Location> implements LocationDAO {

	public void add(Location location) {
		getHibernateTemplate().save(location);
	}

	public Location get(Long id) {
		return (Location) getHibernateTemplate().get(Location.class, id);
	}

	public List<Location> findLocation(String keyword) {
		List result = getHibernateTemplate().find(
				"from Location o where o.info like '%" + keyword + "%'");
		return result;
	}

	public void update(Location location) {
		getHibernateTemplate().update(location);
	}

	public Location getByAccount(String username) {
		return (Location) getHibernateTemplate().find("from Location o where o.account.username='" + username + "'").get(0);
	}
	
	public int searchTotal(String keyword, long selectedProvinceId, long selectedSpecialtyId) {
		String queryString = buildQuery(keyword, selectedProvinceId, selectedSpecialtyId);
		return getHibernateTemplate().find(queryString).size();
	}
	
	@SuppressWarnings("unchecked")
	public List<Location> search(String keyword, int startIndex, int pageSize, long selectedProvinceId, long selectedSpecialtyId) {
		String queryString = buildQuery(keyword, selectedProvinceId, selectedSpecialtyId);
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
		query.setFirstResult(startIndex);
		query.setMaxResults(pageSize);
		return (List<Location>) query.list();
	}

	private String buildQuery(String keyword, long selectedProvinceId,
			long selectedSpecialtyId) {
		String provinceQuery = (selectedProvinceId >= 0) ? " and l.province.provinceId=" + selectedProvinceId : "";
		String specialtyQuery = (selectedSpecialtyId >= 0) ? " and (l.specialty1=" + selectedSpecialtyId + " or l.specialty2=" + selectedSpecialtyId + " or l.specialty3="
				+ selectedSpecialtyId + " or l.specialty4=" + selectedSpecialtyId + " or l.specialty5=" + selectedSpecialtyId + ")" : "";
		String queryString = "from Location l where (l.address like '%" + keyword + "%' or l.info like '%" + keyword + "%' or l.name like '%" + keyword + "%' or l.locationId in"
				+ " (select r.location.locationId from Room r where r.name like '%" + keyword + "%' or r.info like '%" + keyword + "%'))" + provinceQuery + specialtyQuery;
		if (keyword == null || "".equals(keyword)) {
			queryString = "from Location l where 1=1" + provinceQuery + specialtyQuery + " order by CASE WHEN l.account.username IS NOT NULL THEN 1 ELSE 0 END desc, l.rating desc, l.createdDate desc";
		}
		return queryString;
	}
	
	public List<Province> getAllProvince() {
		List result = getHibernateTemplate().find(
				"from Province p order by p.name asc");
		return result;
	}

	public Province getProvince(long id) {
		return (Province) getHibernateTemplate().get(Province.class, id);
	}
	
	public List<Location> getAllLocation() {
		return getHibernateTemplate().find("from Location");
	}
	
	@SuppressWarnings("unchecked")
	public List<Location> getTopLocation(int max) {
		String queryString = "from Location l order by CASE WHEN l.account.username IS NOT NULL THEN 1 ELSE 0 END desc, l.rating desc, l.createdDate desc";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
		query.setFirstResult(0);
		query.setMaxResults(max);
		return (List<Location>) query.list();
	}
}
